use std::error::Error;
use std::fs;
use serde::{Serialize, Deserialize};
use toml;


// server 端配置
#[derive(Debug, Serialize, Deserialize)]
pub struct ServerConfig {
    pub listen_addr: ListenAddress,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListenAddress {
    pub addr: String,
}

// Client
#[derive(Debug, Serialize, Deserialize)]
pub struct ClientConfig {
    pub connect_addr: ConnectAddress,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ConnectAddress {
    pub server_addr: String,
}

impl ServerConfig {
    pub fn load(path: &str) -> Result<Self, Box<dyn Error>> {
        let config = fs::read_to_string(path)?;
        let server_conf: Self = toml::from_str(&config)?;
        Ok(server_conf)
    }
}

impl ClientConfig {
    pub fn load(path: &str) -> Result<Self, Box<dyn Error>> {
        let config = fs::read_to_string(path)?;
        let client_conf: Self = toml::from_str(&config)?;
        Ok(client_conf)
    }
}